package com.esri.android.map.ogc;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.Log;
import com.esri.android.map.DynamicLayer;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.SpatialReference;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class b extends DynamicLayer {
    static final Map<String, Bitmap> u = new HashMap();
    List<a> s;
    final Map<a, Bitmap> t;
    Bitmap v;
    private boolean w;

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public void setUrl(String str) {
    }

    public b() {
        super(null);
        this.w = false;
        this.s = null;
        this.t = Collections.synchronizedMap(new LinkedHashMap());
        this.v = null;
    }

    public void a(final a aVar) {
        if (!this.w) {
            try {
                setDefaultSpatialReference(aVar.d());
                setFullExtent(aVar.c());
                this.supportedSRs = new SpatialReference[]{aVar.d()};
                initLayer();
                this.w = true;
            } catch (Exception unused) {
                Log.w(com.esri.core.internal.a.a, "Fails to initialize image layer!");
            }
        }
        if (aVar.a() == null) {
            return;
        }
        if (this.s == null) {
            this.s = new ArrayList();
        }
        this.s.add(aVar);
        getPoolExecutor().submit(new Runnable() { // from class: com.esri.android.map.ogc.b.1
            @Override // java.lang.Runnable
            public void run() {
                b.this.b(aVar);
            }
        });
    }

    void b(a aVar) {
        try {
            byte[] a = com.esri.core.internal.io.handler.a.a(aVar.a(), (Map<String, String>) null);
            Bitmap decodeByteArray = BitmapFactory.decodeByteArray(a, 0, a.length);
            if (decodeByteArray != null) {
                aVar.b = decodeByteArray.getHeight();
                aVar.a = decodeByteArray.getWidth();
                if (aVar.g() != 0.0f) {
                    Matrix matrix = new Matrix();
                    matrix.setRotate(-aVar.g(), aVar.e() * 0.5f, aVar.f() * 0.5f);
                    RectF rectF = new RectF(0.0f, 0.0f, aVar.e(), aVar.f());
                    matrix.mapRect(rectF);
                    aVar.c = Math.round(rectF.width());
                    aVar.d = Math.round(rectF.height());
                    Bitmap createBitmap = Bitmap.createBitmap(aVar.c, aVar.d, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(createBitmap);
                    matrix.reset();
                    matrix.setTranslate(-rectF.left, -rectF.top);
                    matrix.preRotate(-aVar.g(), aVar.e() * 0.5f, aVar.f() * 0.5f);
                    canvas.drawBitmap(decodeByteArray, matrix, null);
                    Envelope c = aVar.c();
                    RectF rectF2 = new RectF((float) c.getXMin(), (float) c.getYMin(), (float) c.getXMax(), (float) c.getYMax());
                    matrix.reset();
                    matrix.setRotate(aVar.g(), (float) c.getCenterX(), (float) c.getCenterY());
                    matrix.mapRect(rectF2);
                    aVar.e = new Envelope(rectF2.left, rectF2.top, rectF2.right, rectF2.bottom);
                    this.t.put(aVar, createBitmap);
                    decodeByteArray.recycle();
                    return;
                }
                aVar.c = aVar.a;
                aVar.d = aVar.b;
                aVar.e = aVar.c();
                this.t.put(aVar, decodeByteArray);
                return;
            }
            throw new Exception("the image data could not be decoded.");
        } catch (Exception e) {
            Log.w(com.esri.core.internal.a.a, "Fails to fetch the image!", e);
        }
    }

    public a[] c() {
        Map<a, Bitmap> map = this.t;
        if (map == null || map.size() <= 0) {
            return null;
        }
        return (a[]) this.t.keySet().toArray(new a[0]);
    }

    public void d() {
        this.t.clear();
    }

    public void c(a aVar) {
        this.t.remove(aVar);
    }

    @Override // com.esri.android.map.DynamicLayer
    protected byte[] getImage(int i, int i2, Envelope envelope) throws Exception {
        boolean z;
        Canvas canvas;
        boolean z2;
        byte[] bArr;
        if (this.t != null) {
            StringBuilder sb = new StringBuilder();
            sb.append(getWidth());
            sb.append(getHeight());
            String sb2 = sb.toString();
            if (Thread.interrupted()) {
                return null;
            }
            Map<String, Bitmap> map = u;
            synchronized (map) {
                z = true;
                if (Thread.interrupted()) {
                    canvas = null;
                    z2 = true;
                } else {
                    Bitmap bitmap = map.get(sb2);
                    if (bitmap == null) {
                        Bitmap createBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
                        this.v = createBitmap;
                        map.put(sb2, createBitmap);
                    } else {
                        this.v = bitmap;
                    }
                    canvas = new Canvas(this.v);
                    z2 = false;
                }
            }
            if (z2) {
                return null;
            }
            synchronized (this.v) {
                if (Thread.interrupted()) {
                    z2 = true;
                } else {
                    canvas.drawColor(0, PorterDuff.Mode.CLEAR);
                    for (Map.Entry<a, Bitmap> entry : this.t.entrySet()) {
                        a key = entry.getKey();
                        Bitmap value = entry.getValue();
                        if (key.d().getLatestID() == getSpatialReference().getLatestID()) {
                            Envelope envelope2 = key.e;
                            Envelope envelope3 = (Envelope) envelope2.copy();
                            if (envelope3.intersect(envelope)) {
                                canvas.drawBitmap(value, a(envelope3, envelope2, value.getWidth(), value.getHeight()), a(envelope3, envelope, i, i2), (Paint) null);
                            }
                        }
                    }
                }
                if (z2) {
                    return null;
                }
                if (Thread.interrupted()) {
                    bArr = null;
                } else {
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    if (this.v.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)) {
                        bArr = byteArrayOutputStream.toByteArray();
                        byteArrayOutputStream.close();
                    } else {
                        bArr = null;
                    }
                    z = z2;
                }
                if (z) {
                    return null;
                }
                return bArr;
            }
        }
        return null;
    }

    private Rect a(Envelope envelope, Envelope envelope2, int i, int i2) {
        double xMin = (envelope.getXMin() - envelope2.getXMin()) / envelope2.getWidth();
        double d = i;
        Double.isNaN(d);
        int round = (int) Math.round(xMin * d);
        Double.isNaN(d);
        int round2 = (int) Math.round(((envelope.getXMax() - envelope2.getXMin()) / envelope2.getWidth()) * d);
        double yMax = (envelope2.getYMax() - envelope.getYMax()) / envelope2.getHeight();
        double d2 = i2;
        Double.isNaN(d2);
        Double.isNaN(d2);
        return new Rect(round, (int) Math.round(yMax * d2), round2, i2 - ((int) Math.round(((envelope.getYMin() - envelope2.getYMin()) / envelope2.getHeight()) * d2)));
    }
}
